/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Conexao_BD;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Raquel
 */
public class Conexao_BD {

    private String driver = "com.mysql.jdbc.Driver";
    private String url, usuario, senha = new String();
    public static Connection con;

    public Conexao_BD(String endBD, String usr, String pwd) {
        try {
            this.url = "jdbc:mysql://" + endBD;
            this.usuario = usr;
            this.senha = pwd;
            conecta();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Conexao_BD.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Conexao_BD.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void conecta() throws ClassNotFoundException, SQLException{
        Class.forName(this.driver);
        Conexao_BD.con = DriverManager.getConnection(this.url, this.usuario, this.senha);
    }

    public void operacao(String query) throws SQLException{
        System.out.println(query);
        Statement stmt = con.createStatement();
        stmt.execute(query);
    }

    public ResultSet consulta(String query) throws SQLException{
        System.out.println(query);
        Statement stmt = con.createStatement();
        ResultSet result;
        result = stmt.executeQuery(query);
        return result;
    }

    public void desconectar() throws SQLException{
        con.close();
    }

}
